package com.softwinner.bt.Fragment;

import java.util.ArrayList;

import com.softwinner.bt.BtApplication;
import com.softwinner.bt.R;
import com.softwinner.bt.adapter.PairAdapter;
import com.softwinner.bt.bean.PairDevice;
import com.softwinner.bt.util.Command;
import com.softwinner.bt.util.Utils;

import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class PairFragment extends BaseFragment implements OnClickListener{
		
	private Button mF5;
	private Button mBack;
	private Button mDel;
	private ListView mList;	
	private ArrayList<PairDevice> devices;
    private PairAdapter mAdapter;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.bt_list_fragment, container, false);
		initView(view);
		return view;
	}
	
	private BroadcastReceiver receiver = new BroadcastReceiver() {		
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if(Utils.ACTION_UPDATE_PAIRLIST.equals(action)){
				devices = BtApplication.getPairList();
				mAdapter.notifyDataSetChanged();
			}
		}
	};
		
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mBTManager.sendCommand(Command.SEND_QUERY_PAIRE);
        devices = new ArrayList<PairDevice>();
        devices = BtApplication.getPairList();
        mAdapter = new PairAdapter(devices, mActivity);
        mList.setAdapter(mAdapter);
        mList.setOnItemClickListener(mItemClickListener);        
        
        IntentFilter filter = new IntentFilter();
        filter.addAction(Utils.ACTION_UPDATE_PAIRLIST);
        getActivity().registerReceiver(receiver, filter);
	}
	
	private void initView(View view){
		view.findViewById(R.id.list_dialer).setVisibility(View.GONE);
		mF5 = (Button)view.findViewById(R.id.list_f5);
		mBack = (Button)view.findViewById(R.id.list_back);
		mDel = (Button)view.findViewById(R.id.list_del);		
		mList = (ListView)view.findViewById(R.id.bt_list);
		mF5.setOnClickListener(this);
		mBack.setOnClickListener(this);
		mDel.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
        case R.id.list_f5:
        	mBTManager.sendCommand(Command.SEND_QUERY_PAIRE);
            break;
        case R.id.list_back:
            if (devices.size() <= 0) {
                return;
            }
            if(mCurrentPosition>=devices.size()){
                return;
            }
            if(mCurrentPosition < 0)	return;
            mBTManager.sendCommand(Command.SEND_RM_PAIRDEV+mCurrentPosition);
            devices.remove(mCurrentPosition);
            mAdapter.setDataChange(devices);
            mCurrentPosition=-1;
            mAdapter.setSelected(mCurrentPosition);
            break;
        case R.id.list_del:
        	if(mCurrentPosition < 0)	return;
            mBTManager.sendCommand(Command.SEND_RM_PAIRDEV);
            mBTManager.sendCommand(Command.SEND_DIS_CONNECTED);
            devices.clear();
            mAdapter.setDataChange(devices);
            mCurrentPosition=-1;
            mAdapter.setSelected(mCurrentPosition);
            break;
        default:
            break;
        }
	}
	
	private int mCurrentPosition = 0;
    private OnItemClickListener mItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            mCurrentPosition = position;
            mAdapter.setSelected(position);
            mAdapter.notifyDataSetChanged();
        }
    };

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		getActivity().unregisterReceiver(receiver);
		super.onDestroy();		
	}  
}
